//
//  WellComeViewController.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/12.
//  Copyright © 2015年 name. All rights reserved.
//

import SnapKit
import UIKit
import SDWebImage


class WellComeViewController: UIViewController {

    
    override func loadView() {
        view = UIImageView(image: UIImage(named: "ad_background")!)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimate()
    }
    
    private func startAnimate() {
        //usingSpringWithDamping  弹簧系数  0~1之间  越小越弹
        //initialSpringVelocity 加速度
        //弹簧效果 和加速度之间的关系: 弹簧效果 * 10 = 加速度
        
        iconView.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(self.view).offset(-(DcScreenheight * 0.33))
        }
        
        UIView .animateWithDuration(1, delay: 0.25, usingSpringWithDamping: 0.6, initialSpringVelocity: 8.0, options: [], animations: { () -> Void in
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    self.wellComeLable.alpha = 1
                    }, completion: { (_) -> Void in
                    
                            NSNotificationCenter.defaultCenter().postNotificationName(backToHome, object: nil)
                })
        }
        
    }
    
    private let iconView:UIImageView = UIImageView()
    
    private let wellComeLable:UILabel = UILabel(title:UserAccountOAuthModel.shared.Account!.name ?? "" + " 欢迎回来", Color:UIColor.lightGrayColor(),fontSize:15)
    

    private func setupUI() {
                
        self.view.addSubview(iconView)
        self.view.addSubview(wellComeLable)
        
        iconView.sd_setImageWithURL(NSURL(string:UserAccountOAuthModel.shared.Account?.avatar_large ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 50
        iconView.layer.masksToBounds = true
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.centerY.equalTo(self.view)
            make.width.equalTo(100)
            make.height.equalTo(100)
        }
        
        wellComeLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(8.0)
            make.centerX.equalTo(iconView)
        }
        
        wellComeLable.alpha = 0
        
    }
}
